package com.jk.customer.service.impl;

import com.jk.customer.dao.ProductDao;
import com.jk.customer.entity.*;
import com.jk.customer.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    /**
     * 陈浩
     */
    @Autowired
    private ProductDao productDao;

    /**
     * 查询 时间业务判断
     * @param product
     * @param request
     * @return
     * 陈浩
     */
    @Override
    public List<ProductEntity> findProduct(ProductEntity product, HttpServletRequest request) {

        UserEntity userEntity = (UserEntity) request.getSession().getAttribute("user");

        List<ProductEntity> list = productDao.findProduct(product, userEntity.getUserId());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        /**
         * 系统时间
         */
        Date date = new Date();
        long time = date.getTime();
        System.err.println(date);
        /**
         * 开始时间
         */
        for (int i = 0; i < list.size(); i++) {
            String startDate = list.get(i).getQueryPriceStartDate();
            System.err.println(startDate);
            int a= -1;
            try {
                Date parse = sdf.parse(startDate);
                long dateTime = time-parse.getTime();
                long dayTime = dateTime/60/60/1000/24;
                System.err.println((time-parse.getTime())/60/60/1000/24);

                String endDate = list.get(i).getQueryPriceEndDate();
                Date endTime = sdf.parse(endDate);
                Long endDateTime = time - endTime.getTime();
                System.err.println(endDateTime);

                if (list.get(i).getQueryPriceStatus() != 5 && list.get(i).getQueryPriceStatus() != 6 && list.get(i).getQueryPriceStatus() != 7 && list.get(i).getQueryPriceStatus() != 14){
                    if (dayTime < 0 ){
                        a=1;
                        list.get(i).setQueryPriceStatus(a);
                    }else if (dayTime >= 0){
                        a=2;
                        list.get(i).setQueryPriceStatus(a);
                    }else if (dayTime > 0 && endDateTime < 0){
                        a=3;
                        list.get(i).setQueryPriceStatus(a);
                    }else if (endDateTime > 0){
                        a=4;
                        list.get(i).setQueryPriceStatus(a);
                    }
                }
            }
            catch (ParseException e) {
               e.printStackTrace();
            }
            if (list.get(i).getQueryPriceStatus() != 5 && list.get(i).getQueryPriceStatus() != 6 && list.get(i).getQueryPriceStatus() != 7 && list.get(i).getQueryPriceStatus() != 14){
                String id = list.get(i).getProductId();
                productDao.updateStatus(a,id);
            }

        }
        return list;
    }

    @Override
    public void cancelEnquiry(String productId) {
        productDao.cancelEnquiry(productId);
    }

    @Override
    public void resend(String productId) {
        productDao.resend(productId);

        productDao.resendChr(productId);
    }

    @Override
    public PriceEntity postpone(String productId) {
        PriceEntity postpone = productDao.postpone(productId);
        return postpone;
    }

    @Override
    public List<PurchasingDepartmentOrganization> getSelectClassChr() {
        return productDao.getSelectClassChr();
    }

    @Override
    public List<PurchasingDepartmentOrganization> getSelectClassChrNb(Integer pid) {
        return productDao.getSelectClassChrNb(pid);
    }

    @Override
    public List<PurchasingDepartmentOrganization> getSelectClassChrNb2(Integer pid) {
        return productDao.getSelectClassChrNb2(pid);
    }

    @Override
    public List<PurchasingDepartmentOrganization> getSelectClassChrNb3(Integer pid) {
        return productDao.getSelectClassChrNb3(pid);
    }

    @Override
    public void addProduct_Chr(PriceEntity price, HttpServletRequest request) {
        UUID uuid1 = UUID.randomUUID();
        price.setQueryPriceId(uuid1.toString().replaceAll("-",""));
        Object uuid = request.getSession().getAttribute("uuid");
        price.setQueryPriceRequirementType(uuid.toString());

        request.getSession().setAttribute("priceUuid",uuid1.toString().replaceAll("-",""));
        System.err.println("这是陈浩的ID"+uuid1.toString().replaceAll("-",""));

        Date date = new Date();
        if (price.getSelectQueryPriceStartDate()==1){
            price.setQueryPriceStartDate(date.toLocaleString());
        }

        if(price.getSelectBiddingEndDate()==1){
            price.setBiddingEndDate(date.toLocaleString());
        }

        productDao.addProduct_Chr(price);
    }

    @Override
    public void save(String id,String queryEndDate) {
        productDao.save(id,queryEndDate);
    }

    @Override
    public List<OfferFormEntity> showPriceChr(String productId) {
        return productDao.showPriceChr(productId);
    }

    @Override
    public MaterialsEntity selectQuotedChr(String id, String name, String name2) {
        return productDao.selectQuotedChr(id,name,name2);
    }

    @Override
    public OfferFormEntity selectQuotedChr2(String id, String name) {
        return productDao.selectQuotedChr2(id,name);
    }

    @Override
    public MaterialsEntity selectHuiChr(String nbCh, String nbCh2, String nbCh3) {
        return productDao.selectHuiChr(nbCh,nbCh2,nbCh3);
    }

    @Override
    public void submitOrder(Integer totalPrice, OrderFormEntity order, String nbCh2, String nbCh) {

        order.setOrderId(UUID.randomUUID().toString().replaceAll("-",""));

        productDao.submitOrder(totalPrice,order.getOrderId(),nbCh2,nbCh);

        productDao.updateQueryPriceStatus(nbCh2);
    }

    @Override
    public PriceEntity updateSelectQuery(String productId) {
        return productDao.updateSelectQuery(productId);
    }

    @Override
    public void updateSelectQuerySave(ProductEntity price) {
        productDao.updateSelectQuerySave(price);
    }

    @Override
    public List<ProductEntity> findTotalPages_findPrice(ProductEntity product) {
        return productDao.findTotalPages_findPrice(product);
    }
}
